import webpack from 'webpack'
import webpackDevServer from 'webpack-dev-server'

import { ENV } from './ENV'
import config from './webpack.config'

const options = {
  publicPath: config.output.publicPath,
  contentBase: config.output.path,
  hot: true,
  host: 'localhost',
  disableHostCheck: true,
  historyApiFallback: true,
}

webpackDevServer.addDevServerEntrypoints(config, options)
const compiler = webpack(config)

/*
* webdevserver contains express instance, use it for static files
* server.app.use(`${config.output.publicPath}/resources`, express.static(`${PROJECT.SRC.ROOT}/resources`))
*/

const start = () => {
  const server = new webpackDevServer(compiler, options)
  // middlewares && server.app.use(middlewares)
  server.listen(ENV.PORT, ENV.HOST, function () {
    console.log('WEBPACK DEV SERVER START!\n')
  })
}

export default start
